﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using C5;

namespace COC.CEvent.Tools
{
    public class JITAction
    {
        Dictionary<string,MethodInfo> _cache=new Dictionary<string, MethodInfo>(); 
        public T executeMethod<T>(Type clz,object target,string methodName,object[] param) where T:class
        {
            MethodInfo methodInfo;
            Delegate resultValue=null;
            if (!_cache.TryGetValue(methodName, out methodInfo))
            {
                methodInfo = clz.GetType().GetMethod(methodName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                
            }
            if (methodInfo.ReturnType == typeof(T))
            {
                if (methodInfo.GetParameters().Length == 0)
                {
                    resultValue = Delegate.CreateDelegate(typeof(T), target, methodInfo, true);

                    return resultValue as T;
                }
                else
                {
                   
                }
            }
            return resultValue as T;
        }
    }
}
